package org.oslims.client.uicomponents.editbox;

import java.util.HashMap;
import java.util.Map;

import org.oslims.client.uicomponents.inputfields.BooleanInputField;
import org.oslims.client.uicomponents.inputfields.InputField;
import org.oslims.client.uicomponents.inputfields.PasswordInputField;
import org.oslims.client.uicomponents.inputfields.TextInputField;
import org.oslims.domain.BaseUser;

import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public abstract class EditUserBox {

	final DialogBox editUserBox = new DialogBox(false, true);
	final VerticalPanel editUserBoxPanel = new VerticalPanel();

	protected Map<String, InputField> inputFields = new HashMap<String, InputField>();

	final HorizontalPanel buttonLine = new HorizontalPanel();
	final Button saveButton = new Button("Save");
	final Button cancelButton = new Button("Cancel");

	final TextInputField firstNameField = new TextInputField("firstName", "First Name: ", "80px", "200px");
	final TextInputField lastNameField = new TextInputField("lastName", "Last Name: ", "80px", "200px");
	final TextInputField eMailField = new TextInputField("eMail", "E-Mail: ", "80px", "200px");
	final PasswordInputField passwordField = new PasswordInputField("password", "Password: ", "80px", "200px");
	final BooleanInputField activeField = new BooleanInputField("active", "Active: ", "80px", "200px");

	String editedType;

	public EditUserBox() {

		editUserBoxPanel.add(firstNameField);
		inputFields.put(firstNameField.getFieldName(), firstNameField);

		editUserBoxPanel.add(lastNameField);
		inputFields.put(lastNameField.getFieldName(), lastNameField);

		editUserBoxPanel.add(eMailField);
		inputFields.put(eMailField.getFieldName(), eMailField);

		editUserBoxPanel.add(passwordField);
		inputFields.put(passwordField.getFieldName(), passwordField);

		editUserBoxPanel.add(activeField);
		inputFields.put(activeField.getFieldName(), activeField);

		buttonLine.add(saveButton);
		buttonLine.add(cancelButton);

		editUserBox.add(editUserBoxPanel);
	}

	public void hide() {
		editUserBox.hide();
	}

	protected void clear() {
		firstNameField.getInputField().setValue("");
		lastNameField.getInputField().setValue("");
		eMailField.getInputField().setValue("");
		passwordField.getInputField().setValue("");
		activeField.getInputField().setValue(Boolean.FALSE);
	}

	protected void showEdit(BaseUser b) {
		// firstName.setText(b.getFirstName());
		firstNameField.getInputField().setValue(b.getFirstName());
		lastNameField.getInputField().setValue(b.getLastName());
		eMailField.getInputField().setValue(b.getEMail());
		passwordField.getInputField().setValue(b.getPassword());
		activeField.getInputField().setValue(b.getActive());
	}

	protected void showError(String fieldName, String message) {
		inputFields.get(fieldName).showError(message);
	}

	protected void clearError(String fieldName) {
		inputFields.get(fieldName).clearError();
	}

	protected void clearAllErrors() {
		for (InputField inputField : inputFields.values()) {

			inputField.clearError();
		}
	}

	public HasValue<String> getFirstName() {
		return firstNameField.getInputField();
	}

	public HasValue<String> getLastName() {
		return lastNameField.getInputField();
	}

	public HasValue<String> getEMail() {
		return eMailField.getInputField();
	}

	public HasValue<String> getPassword() {
		return passwordField.getInputField();
	}

	public HasValue<Boolean> getActive() {
		return activeField.getInputField();
	}

	public HasClickHandlers getSaveButton() {
		return saveButton;
	}

	public HasClickHandlers getCancelButton() {
		return cancelButton;
	}
}
